package com.bfly.mall.system.controller;

import com.bfly.common.ResponseUtil;
import com.bfly.common.page.Pager;
import com.bfly.common.util.DataConvertUtils;
import com.bfly.core.base.controller.BaseController;
import com.bfly.core.base.entity.ResponseData;
import com.bfly.core.context.PagerThreadLocal;
import com.bfly.mall.admin.service.IAdminLoginLogService;
import com.bfly.mall.system.entity.SysLog;
import com.bfly.mall.system.service.ISysLogService;
import com.bfly.mall.user.service.IUserLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * 系统日志
 *
 * @author 长沙商脉科技有限公司
 * @date 2020/9/17 21:29
 */
@RestController
@RequestMapping(value = "/manage/logs")
public class SysLogController extends BaseController {

    @Autowired
    private ISysLogService logService;
    @Autowired
    private IAdminLoginLogService loginLogService;
    @Autowired
    private IUserLogService userLogService;

    /**
     * 系统日志列表
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/9/17 21:30
     */
    @GetMapping(value = "/sys")
    public void listSysLogs(HttpServletResponse response, HttpServletRequest request) {
        PagerThreadLocal.set(request);

        Map<String, Object> param = new HashMap<>(1);
        String userName = request.getParameter("name");
        if (StringUtils.hasText(userName)) {
            param.put("user_name", userName);
        }
        Pager<SysLog> pager = logService.getPage(param);
        ResponseUtil.writeJson(response, ResponseData.getSuccess(pager));
    }

    /**
     * 用户操作日志
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/12/24 13:26
     */
    @GetMapping(value = "/user")
    public void listUserLogs(HttpServletResponse response, HttpServletRequest request) {
        PagerThreadLocal.set(request);

        Map<String, Object> param = new HashMap<>(1);
        String userName = request.getParameter("name");
        if (StringUtils.hasText(userName)) {
            param.put("user_name", userName);
        }
        Pager<SysLog> pager = userLogService.getPage(param);
        ResponseUtil.writeJson(response, ResponseData.getSuccess(pager));
    }


    /**
     * 登录日志列表
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/12/24 11:20
     */
    @GetMapping(value = "/login")
    public void listLoginLogs(HttpServletResponse response, HttpServletRequest request) {
        PagerThreadLocal.set(request);

        Map<String, Object> param = new HashMap<>(1);
        String userName = request.getParameter("name");
        if (StringUtils.hasText(userName)) {
            param.put("user_name", userName);
        }
        String isLogin = request.getParameter("isLogin");
        if (StringUtils.hasText(isLogin)) {
            param.put("is_login", DataConvertUtils.convertToBoolean(isLogin));
        }
        Pager<SysLog> pager = loginLogService.getPage(param);
        ResponseUtil.writeJson(response, ResponseData.getSuccess(pager));
    }
}
